package com.mulanweb.api.module.controller;

import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.model.City;
import com.mulanweb.api.module.model.ScenicSpot;
import com.mulanweb.api.module.model.Ticket;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.Date;

/**
 * Created by Administrator on 2016/8/19.
 */
@ValidateFile
public class ScenicSpotController extends BaseController {

    @ValidateForm
    public void save() {
        String scenicSpotName = getPara("scenicName");
        String description = getPara("description");
        Long cityId = getParaToLong("cityId");
        String longitude = getPara("longitude");
        String latitude = getPara("latitude");
        String position = getPara("position");
        ScenicSpot scenicSpot = ScenicSpot.dao.findFirst("select * from scenic_spot where scenic_name = ? and city_id=?",scenicSpotName,cityId);
        if(scenicSpot !=null){
            returnJson(0,"景点已存在");
        }else{
            scenicSpot = new ScenicSpot();
            scenicSpot.setScenicName(scenicSpotName);
            scenicSpot.setDescription(description);
            scenicSpot.setPosition(position);
            scenicSpot.setCityId(cityId);
            scenicSpot.setLongitude(longitude);
            scenicSpot.setLatitude(latitude);
            scenicSpot.setCreateTime(new Date());
            if(scenicSpot.save()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }
    }

    @ValidateForm
    public void update(){
        String scenicSpotName = getPara("scenicName");
        String description = getPara("description");
        Long cityId = getParaToLong("cityId");
        String longitude = getPara("longitude");
        String latitude = getPara("latitude");
        String position = getPara("position");
        Long scenicId = getParaToLong("scenicId");

        ScenicSpot scenicSpot = ScenicSpot.dao.findById(scenicId);
        if(scenicSpot !=null){
            scenicSpot.setScenicName(scenicSpotName);
            scenicSpot.setDescription(description);
            scenicSpot.setCityId(cityId);
            scenicSpot.setLongitude(longitude);
            scenicSpot.setLatitude(latitude);
            scenicSpot.setPosition(position);
            scenicSpot.update();
            returnSuccessJson();
        }else{
            returnFailureJson();
        }
    }

    @ValidateForm
    public void search() {
        SQLGenerateHelper helper = new SQLGenerateHelper("scenic_spot","create_time desc");
        helper.addLikeCondition("scenic_name",getPara("name"));
        Page<ScenicSpot> page = ScenicSpot.dao.paginate(getPageNumber(),getPageSize(),helper.getSqlSelect() , helper.getSqlExceptSelect(), helper.getQueryParas());
        returnPageModelJson(page);
    }
}
